<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Write a field array as a CSV line</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fpassthru.html">SplFileObject::fpassthru</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div>
 <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="splfileobject.fputcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplFileObject::fputcsv</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0)</p><p class="refpurpose"><span class="refname">SplFileObject::fputcsv</span> &mdash; <span class="dc-title">Write a field array as a CSV line</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-splfileobject.fputcsv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span> <span class="methodname"><strong>SplFileObject::fputcsv</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$fields</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code><span class="initializer"> = &#039;,&#039;</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &#039;&quot;&#039;</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Writes the <em><code class="parameter">fields</code></em> array to the file as a CSV line.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-splfileobject.fputcsv-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">fields</code></em></dt>

    <dd>

     <p class="para">
      An array of values.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">delimiter</code></em></dt>

    <dd>

     <p class="para">
      The optional <em><code class="parameter">delimiter</code></em> parameter sets the field
      delimiter (one character only).
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">enclosure</code></em></dt>

    <dd>

     <p class="para">
      The optional <em><code class="parameter">enclosure</code></em> parameter sets the field
       enclosure (one character only).
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-splfileobject.fputcsv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the length of the written string 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
  <p class="para">
   Returns <strong><code>FALSE</code></strong>, and does not write the CSV line to the file, if the
   <em><code class="parameter">delimiter</code></em> or <em><code class="parameter">enclosure</code></em>
   parameter is not a single character.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-splfileobject.fputcsv-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   An <strong><code>E_WARNING</code></strong> level error is issued if the
   <em><code class="parameter">delimiter</code></em> or <em><code class="parameter">enclosure</code></em>
   parameter is not a single character.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-splfileobject.fputcsv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="splfileobject.fputcsv.examples.basic">
    <p><strong>Example #1 <span class="methodname"><strong>SplFileObject::fputcsv()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$list&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'aaa'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bbb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ccc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dddd'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'456'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'789'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'"aaa"'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'"bbb"'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'file.csv'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$list&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$fields</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fputcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will write the following to <em>file.csv</em>:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
aaa,bbb,ccc,dddd
123,456,789
&quot;&quot;&quot;aaa&quot;&quot;&quot;,&quot;&quot;&quot;bbb&quot;&quot;&quot;

</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-splfileobject.fputcsv-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fputcsv.html" class="function" rel="rdfs-seeAlso">fputcsv()</a> - 将行格式化为 CSV 并写入文件指针</span></li>
    <li class="member"><span class="methodname"><a href="splfileobject.fgetcsv.html" class="methodname" rel="rdfs-seeAlso">SplFileObject::fgetcsv()</a> - Gets line from file and parse as CSV fields</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="splfileobject.fpassthru.html">SplFileObject::fpassthru</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfileobject.fscanf.html">SplFileObject::fscanf</a></div>
 <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
